// pages/newAddItem/newAddItem.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: true,
    show1: true,
    show2: true,
    show3: true,
    show4: true,
    show5: true,
    isMask:false,
    date:'',
    lists:[],
    proLinkperson:'',
    proAddress:'',
    deptLinkperson:'',
    deptPhone:'',
    hostIndex:'',
    typeIndex:'',
    sourceList: ['街道上报', '领导批示', '现场调度', '现场反馈', '其他'],
    typeList: ['资金', '规划', '土地', '拆迁', '节能', '供水', '环评', '供电', '交通设施', '审批事项', '其他'],
    hostList:['纪委','组织部','明光路街道','胜利路街道','三里街街道','铜陵路街道','七里站街道','红光街道','和平路街道','城东街道','长淮街道','方庙街道','嘉山路街道','大兴镇','龙岗综合经济开发区'],
    coordinateList: ['光明路街道', '胜利路街道', '三里街街道', '铜陵路街道', '七里站街道', '红光街道', '和平路街道', '城东街道', '长淮街道', '方庙街道', '嘉山路街道', '大兴镇', '龙岗综合经济开发区','组织部','宣传部','统战部','政法委','政研室','巡察办','老干局','区机关事务管理中心','区园林绿化管理中心','区投资促进中心','区重点工程建设管理中心','发展和改革委员会','教育体育局','科学技术局','经济和信息化局','民政局','司法局','财政局','人力资源和社会保障局','住房和城乡建设局','农林水务局','商务局','文化和旅游局','卫生健康委员会','退役军人事务局','应急管理局','审计局','城市管理局','市场监督管理局','统计局','医疗保障局','信访局','数据资源局','政务服务管理局'],
    index:'',
    sourceIndex: '',
    coordinateCheck:[],
    detailPics:[],
    problemText:'',
    add: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.thirdScript.http.post('project/list', {  }, 'list', this)
  },
  list:function(res){
    if(res.code == 200){
      wx.setStorageSync('projectList', res.content.list)
      var projectList = []
      for (let i in res.content.list) {
        projectList.push(res.content.list[i].projectName)
      }
      this.setData({
        lists: projectList
      })
    }
  },
  // 项目名称
   bindPickerChange: function (e) {
    this.setData({
      show: false,
      index: e.detail.value
    })
     app.thirdScript.http.post('project/detail', { projectId: wx.getStorageSync('projectList')[this.data.index].id }, 'projectDetail', this)
  },
  projectDetail:function(res){
    if(res.code == 200){
      this.setData({
        proLinkperson: res.content.proLinkperson,
        proAddress: res.content.proAddress,
        deptLinkperson: res.content.deptLinkperson,
        deptPhone: res.content.deptPhone
      })
    }
  },
  // 拨打电话
  dial:function(){
    wx.makePhoneCall({
      phoneNumber: this.data.deptPhone,
    })
  },
  // 选择事项来源
  bindSourceChange: function (e) {
    this.setData({
      show1: false,
      sourceIndex: e.detail.value
    })
  },
  // 规定时限
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      show2: false,
      date: e.detail.value
    })
  },

  // 主管单位
  bindHostChange: function (e) {
    this.setData({
      show3: false,
      hostIndex: e.detail.value
    })
  },
  coordinate: function () {
    var that = this
    that.setData({
      show5: false,
      isMask: !(that.data.isMask)
    })
  },
  cancel:function(){
    var that = this
    that.setData({
      isMask: !(that.data.isMask)
    })
  },
  ensure: function () {
    var that = this
    that.setData({
      isMask: !(that.data.isMask)
    })
  },
  checkboxChange: function (e) {
    this.setData({
      coordinateCheck: e.detail.value
    })
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
  },
  // 问题类别
  bindTypeChange: function (e) {
    this.setData({
      show4: false,
      typeIndex: e.detail.value
    })
  },
  modify: function (e) {
    var _this = this;
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log(res.tempFilePaths)
        if (res.tempFilePaths.length >=5){
          _this.setData({
            add: false
          })
        }
        // const tempFilePaths = res.tempFilePaths
        _this.setData({
          detailPics: res.tempFilePaths
        })
      }
    })
  },
  deleteImage: function (e) {
    var that = this;
    var images = that.data.detailPics;
    var index = e.currentTarget.dataset.index;//获取当前长按图片下标
    if (images.length < 5) {
      that.setData({
        add: true
      })
    }
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          images.splice(index, 1);
        } else if (res.cancel) {
          return false;
        }
        that.setData({
          detailPics: images
        });
      }
    })
  },
  problem:function(e){
    this.setData({
      problemText: e.detail.value
    })
  },
  submit:function(){
    var data = {
      proId: wx.getStorageSync('projectList')[this.data.index].id,
      source: this.data.sourceList[this.data.sourceIndex],
      limitedTime: this.data.date,
      competentUnits: this.data.hostList[this.data.hostIndex],
      participatingUnits: this.data.coordinateCheck,
      problemType: this.data.typeList[this.data.typeIndex],
      problem: this.data.problemText,
      file: this.data.detailPics, 
      comming: wx.getStorageSync('userInfo').loginName
    }
    app.thirdScript.http.post('issue/issue/month/reply', data, 'reply', this)
  },
  reply:function(res){
    if(res.code == 200){
      wx.showToast({
        title: res.message,
      })
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];
      wx.navigateBack({
        delta: 1,
      })
      prevPage.onLoad();
    }else{
      wx.showToast({
        title: res.message,
        icon: 'none',
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})